package com.rpframework.website.device.act.api;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.rpframework.core.BaseAct;
import com.rpframework.core.utils.TagUtils;
import com.rpframework.utils.NumberUtils;
import com.rpframework.utils.Pager;
import com.rpframework.website.device.domain.Estate;
import com.rpframework.website.device.domain.Interact;
import com.rpframework.website.device.domain.Ugc;
import com.rpframework.website.device.domain.User;
import com.rpframework.website.device.exception.APICodeException;
import com.rpframework.website.device.service.EstateService;
import com.rpframework.website.device.service.UgcService;
import com.rpframework.website.device.service.PraiseService;
import com.rpframework.website.device.service.TCommentService;
import com.rpframework.website.device.service.UserService;
/**
 * 互动api类
 * @author zhangli
 * @date 2016年3月17日 上午10:07:52
 */
@Controller
@RequestMapping("/api/Ugc")
public class UgcApiAct extends BaseAct{
	@Resource UgcService service;
	@Resource PraiseService praiseService;
	@Resource TCommentService tcommentService;
	@Resource UserService userService;
	@Resource EstateService estateService;
	 Gson gson=new Gson();
	 
	/* 按type查询小区活动 办事指南 曝光台 跳蚤市场  按用户查询我的互动 */
	/**
	 * 查看互动列表 我发布的互动
	 * @param pager
	 * @param type
	 * @param my
	 * @param all
	 * @param callback
	 * @param session
	 * @return
	 * @time 2016年3月17日 上午11:26:29
	 */
	
	@RequestMapping(value="/detail" , produces = "application/json; charset=utf-8")
	public @ResponseBody String detail(	
			@RequestParam(value="pager",required = false ) Pager<Interact> pager,
			@RequestParam(value="id",required = false ) Integer id,//查我发布的
			@RequestParam(value = "callback",required = false ) String callback,
			HttpSession session
			){
		User user = getSessionUser(session);
	    if (user == null) {
	        throw new APICodeException(-4, "你还没登陆!");
	    }
	    if(NumberUtils.isNotValid(id)){
	    	throw new APICodeException(-6, "id为必填参数!");
	    }
		if(NumberUtils.isNotValid(id))//如果参数有my 就是查我发布的
	pager.getSearchMap().put("userId", String.valueOf(user.getId()));
		System.out.println(pager);
		
    	Ugc t = service.select(id);
    	System.out.println(t);
    	JsonObject obj = new JsonObject();
    	if(t!=null && t.getUserId()!=null){
			obj.addProperty("userId",t.getUserId());
			User u = userService.select(t.getUserId());
			if(u==null){
				
			}//如果发布的用户不存在了，这个信息也不要显示了
			;
			obj.addProperty("id", t.getId());//查询当条数据id
			obj.addProperty("type",t.getType());//关联类型
			obj.addProperty("uname", u.getNickName());//昵称
			obj.addProperty("uimg",TagUtils.getFileFullPath(u.getHeadImg()) );//头像
		}else{
			obj.addProperty("code", -1);
			obj.addProperty("msg", "用户或互动无效！");
		}
		obj.addProperty("time",TagUtils.formatDate(t.getCreateTime()));//创建时间
		obj.addProperty("info",t.getInfo());//内容
		obj.add("urlList", service.getUrlListFormat(t.getUrlList()));//图片数组
		
		obj.addProperty("comment", tcommentService.queryAll(t.getId(),t.getType()));//评论总数
		obj.addProperty("favoriteCount",praiseService.priceCount(t.getId(), t.getType(),2));//评论表里type 查点赞总数  status=2 为收藏  status=3为点赞
		obj.addProperty("isFavorite",praiseService.isPraiseByUser(t.getId(), t.getType(), user.getId()));// 1可以收藏  2以收藏
		obj.addProperty("praiseCount",praiseService.priceCount(t.getId(), t.getType(),3));//评论表里type 查点赞总数
		obj.addProperty("isPraise",praiseService.isPraiseByUser(t.getId(), t.getType(), user.getId()));// 1可以点赞 2不能点赞
		return (callback + "(" + obj + ")");
	}
	@RequestMapping(value="/add" , produces = "application/json; charset=utf-8")
	public @ResponseBody String add(	
			@RequestParam(value = "callback",required = false ) String callback,
			@RequestParam(value = "info",required = false ) String info,//内容
			@RequestParam(value = "urlList",required = false ) String urlList,//图片地址
			@RequestParam(value = "address",required = false ) String address,//当前用户位置
			HttpSession session
			){
		User user = getSessionUser(session);
		if (user == null) {
			throw new APICodeException(-4, "你还没登陆!");
		}
		if(StringUtils.isBlank(info) || StringUtils.isBlank(urlList) ||StringUtils.isBlank(address) ){
			throw new APICodeException(-9, "参数列表错误!");
		}
		Ugc t = new Ugc();
		t.setUrlList(urlList);
		t.setType(8);//跳蚤市场
		t.setCreateTime(System.currentTimeMillis()/1000);
		t.setState(1);
		t.setUserId(user.getId());
		Estate e = estateService.select(user.getEstateId());
		boolean flag = service.insert(t);
		JsonObject obj = new JsonObject();
		if(flag){
			obj.addProperty("code", 200);
			obj.addProperty("msg", "添加成功");
		}else{
			obj.addProperty("code", 500);
			obj.addProperty("msg", "添加失败");
		}
		return (callback + "(" + obj + ")");
	}
}